package com.fyzk.activity.lottery.jc.football;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.activity.app.App;
import com.fyzk.activity.app.AppToast;
import com.fyzk.activity.lottery.jc.football.newJz.data.NewScoreDetailActivity;
import com.fyzk.bean.JCZQResponseInfo;
import com.fyzk.net.Constants;
import com.fyzk.net.PublicMethod;
import com.fyzk.utils.AppUtil;
import com.tyjc.lottery.R;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.fyzk.activity.lottery.jc.football.newJz.JCFootballBaseFragment.INITSPINNER;


/**
 * 竞彩的适配器
 */
public class JcInfoAdapter extends BaseExpandableListAdapter {
    private String TAG = this.getClass().getSimpleName();

    private Activity activity;
    private Context context;
    private LayoutInflater mInflater; // 扩充主列表布局
    private List<JCZQResponseInfo> mList = new ArrayList<>();
    private List<String> betcodes;
    private Handler handler;
    private int count;
    private int maxCount = 10;

    private boolean isDG;


    public JcInfoAdapter(Activity activity, Context context, List<JCZQResponseInfo> list,
                         List<String> betcodes, Handler handler, boolean isDG) {
        this.activity = activity;
        this.context = context;
        mInflater = LayoutInflater.from(context);
        this.mList = list;
        this.betcodes = betcodes;
        this.handler = handler;
        this.isDG = isDG;
        count = 0;

        if (App.isBaskOrder) {
            maxCount = 8;
        } else {
            maxCount = 10;
        }
    }


    int index;

    public void onclikBtn() {
        count = 0;
        betcodes = new ArrayList<>();
        for (int i = 0; i < mList.size(); i++) {
            for (JCInfo info : mList.get(i).matchDatas) {
                if (info.getOnclikNum() > 0) {
                    count++;
                    betcodes.add("" + info.getOnclikNum());
                }
            }

        }

        Message msg = handler.obtainMessage();
        msg.obj = count;
        msg.what = INITSPINNER;
        handler.sendMessage(msg);
    }

    public void setDG(boolean DG) {
        this.isDG = DG;
        notifyDataSetChanged();
    }

    class ViewHolder {
        LinearLayout xianShi;
        LinearLayout ll_xiangqing;
        ImageView ivIsDanGuan;
        TextView team;
        TextView num;
        TextView home;
        TextView away;
        TextView score;
        TextView timeEnd;
        CheckBox btn1;
        CheckBox btn2;
        CheckBox btn3;
        LinearLayout jz_ll_spf;
        LinearLayout jz_ll_rspf;
        TextView jz_stop_spf;
        TextView jz_stop_rspf;


        public CheckBox btn1_r;
        public CheckBox btn2_r;
        public CheckBox btn3_r;
    }

    @Override
    public int getGroupCount() {
        return mList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mList.get(groupPosition).matchDatas.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return mList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mList.get(groupPosition).matchDatas.get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return groupPosition * 100 + childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup
            parent) {
        JCZQResponseInfo info = mList.get(groupPosition);
        JcInfoAdapter.GroupHolder groupHolder = null;
        if (convertView == null) {
            convertView = View.inflate(activity, R.layout.jclq_adapter_group, null);
            groupHolder = new JcInfoAdapter.GroupHolder();
            groupHolder.date_tv = convertView
                    .findViewById(R.id.jclq_group_tv);
            groupHolder.jianTou_iv = convertView
                    .findViewById(R.id.jclq_group_iv);
            convertView.setTag(groupHolder);
        } else {
            groupHolder = (JcInfoAdapter.GroupHolder) convertView.getTag();
        }

        groupHolder.date_tv.setText(info.matchTime + "   " + info.matchWeekday
                + "    共 " + info.matchDatas.size() + " 场");
        if (isExpanded) {
            groupHolder.jianTou_iv.startAnimation(AnimationUtils.loadAnimation(
                    context, R.anim.title_jiantou_shun));
        } else {
            groupHolder.jianTou_iv.startAnimation(AnimationUtils.loadAnimation(
                    context, R.anim.title_jiantou_ni));
        }

        return convertView;
    }

    class GroupHolder {
        TextView date_tv;
        ImageView jianTou_iv;
    }

    private float teamNameTvDefaultTSize = 0;//赛事主客队默认的字体大小

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View
            convertView, ViewGroup parent) {
        index = childPosition;
        final JCInfo infor = mList.get(groupPosition).matchDatas.get(childPosition);
        final ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.jz_item_spf,
                    null);
            holder = new ViewHolder();

            holder.xianShi = convertView.findViewById(R.id
                    .jc_main_list_item_xianshi);
            holder.ll_xiangqing = convertView.findViewById(R.id
                    .jc_main_list_item_yincang);

            holder.ivIsDanGuan = convertView.findViewById(R.id.ivIsDanGuan);

            holder.team = convertView
                    .findViewById(R.id.jc_main_list_item_text_team);
            holder.num = convertView
                    .findViewById(R.id.jc_main_list_item_text_num);
            holder.home = convertView
                    .findViewById(R.id.jc_main_list_item_text_team_name1);
            holder.away = convertView
                    .findViewById(R.id.jc_main_list_item_text_team_name2);
            teamNameTvDefaultTSize = holder.away.getTextSize() / TypedValue.COMPLEX_UNIT_SP;
            holder.score = convertView
                    .findViewById(R.id.txt_rf);
            holder.timeEnd = convertView
                    .findViewById(R.id.jc_main_list_item_text_time_end);
            holder.btn1 = convertView
                    .findViewById(R.id.jc_main_list_item_button1);
            holder.btn2 = convertView
                    .findViewById(R.id.jc_main_list_item_button2);
            holder.btn3 = convertView
                    .findViewById(R.id.jc_main_list_item_button3);
            holder.btn1_r = convertView
                    .findViewById(R.id.jc_main_list_item_button1_r);
            holder.btn2_r = convertView
                    .findViewById(R.id.jc_main_list_item_button2_r);
            holder.btn3_r = convertView
                    .findViewById(R.id.jc_main_list_item_button3_r);
            holder.jz_ll_spf = convertView
                    .findViewById(R.id.jz_ll_spf);
            holder.jz_ll_rspf = convertView
                    .findViewById(R.id.jz_ll_rspf);
            holder.jz_stop_spf = convertView
                    .findViewById(R.id.jz_stop_spf);
            holder.jz_stop_rspf = convertView
                    .findViewById(R.id.jz_stop_rspf);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.team.setBackgroundResource(AppUtil.getTeamNameColor(infor.getMatchName()));
        holder.team.setText(infor.getMatchName());

        holder.num.setText(PublicMethod.getTeamNum(infor.getMatchNum()));
        holder.home.setText(infor.getHomeTeam());
        holder.away.setText(infor.getAwayTeam());
        String a = infor.getHandicap();
        holder.score.setText("让\n" + a);
        if (a.contains("-")) {
            holder.score.setBackgroundColor(activity.getResources().getColor(R.color.jczq_hh_r_bg));
        } else {
            holder.score.setBackgroundColor(activity.getResources().getColor(R.color.jczq_hh_l_bg));
        }
        holder.btn1.setText(getShowSP("胜 " + infor.getVs_3()));
        holder.btn2.setText(getShowSP("平 " + infor.getVs_1()));
        holder.btn3.setText(getShowSP("负 " + infor.getVs_0()));

        holder.btn1_r.setText(getShowSP("让胜 " + infor.getLetvs_3()));
        holder.btn2_r.setText(getShowSP("让平 " + infor.getLetvs_1()));
        holder.btn3_r.setText(getShowSP("让负 " + infor.getLetvs_0()));

//        if (infor.isDan()){
//            holder.ivIsDanGuan.setVisibility(View.VISIBLE);
//        }else{
//            holder.ivIsDanGuan.setVisibility(View.GONE);
//        }

        // 判断是否开售(赔率为空,禁止点击)
        if (isDG) {
            if (infor.getDgStaticSaleSpfWrqStatus() == 1) {//胜平负单关停售
                holder.jz_ll_spf.setVisibility(View.GONE);
                holder.jz_stop_spf.setVisibility(View.VISIBLE);
            } else {
                holder.jz_ll_spf.setVisibility(View.VISIBLE);
                holder.jz_stop_spf.setVisibility(View.GONE);
                holder.btn1.setChecked(infor.btnHGPojo.get(infor.VS_3).isSel);
                holder.btn2.setChecked(infor.btnHGPojo.get(infor.VS_1).isSel);
                holder.btn3.setChecked(infor.btnHGPojo.get(infor.VS_0).isSel);
            }
            if (infor.getDgStaticSaleSpfStatus() == 1) {//rq胜平负单关停售
                holder.jz_ll_rspf.setVisibility(View.GONE);
                holder.jz_stop_rspf.setVisibility(View.VISIBLE);
            } else {
                holder.jz_ll_rspf.setVisibility(View.VISIBLE);
                holder.jz_stop_rspf.setVisibility(View.GONE);
                holder.btn1_r.setChecked(infor.btnHGPojo.get(infor.LETVS_3).isSel);
                holder.btn2_r.setChecked(infor.btnHGPojo.get(infor.LETVS_1).isSel);
                holder.btn3_r.setChecked(infor.btnHGPojo.get(infor.LETVS_0).isSel);
            }

        } else {//过关
            if (infor.getStaticSaleSpfWrqStatus() == 1) {//胜平负过关停售
                holder.jz_ll_spf.setVisibility(View.GONE);
                holder.jz_stop_spf.setVisibility(View.VISIBLE);
            } else {
                holder.jz_ll_spf.setVisibility(View.VISIBLE);
                holder.jz_stop_spf.setVisibility(View.GONE);
                holder.btn1.setChecked(infor.btnHGPojo.get(infor.VS_3).isSel);
                holder.btn2.setChecked(infor.btnHGPojo.get(infor.VS_1).isSel);
                holder.btn3.setChecked(infor.btnHGPojo.get(infor.VS_0).isSel);
            }
            if (infor.getStaticSaleSpfStatus() == 1) {//rq胜平负过关停售
                holder.jz_ll_rspf.setVisibility(View.GONE);
                holder.jz_stop_rspf.setVisibility(View.VISIBLE);

            } else {

                holder.jz_ll_rspf.setVisibility(View.VISIBLE);
                holder.jz_stop_rspf.setVisibility(View.GONE);
                holder.btn1_r.setChecked(infor.btnHGPojo.get(infor.LETVS_3).isSel);
                holder.btn2_r.setChecked(infor.btnHGPojo.get(infor.LETVS_1).isSel);
                holder.btn3_r.setChecked(infor.btnHGPojo.get(infor.LETVS_0).isSel);
            }

        }
        SimpleDateFormat format_ = new SimpleDateFormat("HH:mm");
        holder.timeEnd.setText(format_.format(new Date(infor.getEndSaleTime())) + "[截]");

        onclikBtn();
        holder.btn1.setTag(infor.btnHGPojo.get(infor.VS_3));
        holder.btn2.setTag(infor.btnHGPojo.get(infor.VS_1));
        holder.btn3.setTag(infor.btnHGPojo.get(infor.VS_0));
        holder.btn1_r.setTag(infor.btnHGPojo.get(infor.LETVS_3));
        holder.btn2_r.setTag(infor.btnHGPojo.get(infor.LETVS_1));
        holder.btn3_r.setTag(infor.btnHGPojo.get(infor.LETVS_0));


        holder.btn1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                if (infor.getOnclikNum() == 0 && count == maxCount) {
                    AppToast.getToast().show("最多选择" + maxCount + "场比赛");
                    infor.btnHGPojo.get(infor.VS_3).isSel = false;
                    JcInfoAdapter.this.notifyDataSetChanged();
                    return;
                } else {
                    boolean isWin = infor.btnHGPojo.get(infor.VS_3).isSel;
                    infor.btnHGPojo.get(infor.VS_3).isSel = !isWin;
                    if (infor.btnHGPojo.get(infor.VS_3).isSel == true) {

                        infor.setOnclikNum(infor.getOnclikNum() + 1);
                    } else {
                        infor.setOnclikNum(infor.getOnclikNum() - 1);
                    }
                    onclikBtn();
                }
            }
        });
        holder.btn2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                if (infor.getOnclikNum() == 0 && count == maxCount) {
                    AppToast.getToast().show("最多选择" + maxCount + "场比赛");
                    infor.btnHGPojo.get(infor.VS_1).isSel = (false);
                    JcInfoAdapter.this.notifyDataSetChanged();
                    return;
                } else {
                    boolean isLevel = infor.btnHGPojo.get(infor.VS_1).isSel;
                    infor.btnHGPojo.get(infor.VS_1).isSel = !isLevel;
                    if (infor.btnHGPojo.get(infor.VS_1).isSel == true) {
                        infor.setOnclikNum(infor.getOnclikNum() + 1);
                    } else {
                        infor.setOnclikNum(infor.getOnclikNum() - 1);
                    }
                    onclikBtn();
                }

            }
        });
        holder.btn3.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                if (infor.getOnclikNum() == 0 && count == maxCount) {
                    AppToast.getToast().show("最多选择" + maxCount + "场比赛");
                    infor.btnHGPojo.get(infor.VS_0).isSel = (false);
                    JcInfoAdapter.this.notifyDataSetChanged();
                    return;
                } else {
                    boolean isFail = infor.btnHGPojo.get(infor.VS_0).isSel;
                    infor.btnHGPojo.get(infor.VS_0).isSel = !isFail;
                    if (infor.btnHGPojo.get(infor.VS_0).isSel == true) {
                        infor.setOnclikNum(infor.getOnclikNum() + 1);
                    } else {
                        infor.setOnclikNum(infor.getOnclikNum() - 1);
                    }
                    onclikBtn();
                }
            }
        });
        holder.btn1_r.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                if (infor.getOnclikNum() == 0 && count == maxCount) {
                    AppToast.getToast().show("最多选择" + maxCount + "场比赛");
                    infor.btnHGPojo.get(infor.LETVS_3).isSel = false;
                    JcInfoAdapter.this.notifyDataSetChanged();
                    return;
                } else {
                    boolean isWin = infor.btnHGPojo.get(infor.LETVS_3).isSel;
                    infor.btnHGPojo.get(infor.LETVS_3).isSel = !isWin;
                    if (infor.btnHGPojo.get(infor.LETVS_3).isSel == true) {
                        infor.setOnclikNum(infor.getOnclikNum() + 1);
                    } else {
                        infor.setOnclikNum(infor.getOnclikNum() - 1);
                    }
                    onclikBtn();
                }
            }
        });
        holder.btn2_r.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                if (infor.getOnclikNum() == 0 && count == maxCount) {
                    AppToast.getToast().show("最多选择" + maxCount + "场比赛");
                    infor.btnHGPojo.get(infor.LETVS_1).isSel = (false);
                    JcInfoAdapter.this.notifyDataSetChanged();
                    return;
                } else {
                    boolean isLevel = infor.btnHGPojo.get(infor.LETVS_1).isSel;
                    infor.btnHGPojo.get(infor.LETVS_1).isSel = !isLevel;
                    if (infor.btnHGPojo.get(infor.LETVS_1).isSel == true) {
                        infor.setOnclikNum(infor.getOnclikNum() + 1);
                    } else {
                        infor.setOnclikNum(infor.getOnclikNum() - 1);
                    }
                    onclikBtn();
                }

            }
        });
        holder.btn3_r.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                if (infor.getOnclikNum() == 0 && count == maxCount) {
                    AppToast.getToast().show("最多选择" + maxCount + "场比赛");
                    infor.btnHGPojo.get(infor.LETVS_0).isSel = (false);
                    JcInfoAdapter.this.notifyDataSetChanged();
                    return;
                } else {
                    boolean isFail = infor.btnHGPojo.get(infor.LETVS_0).isSel;
                    infor.btnHGPojo.get(infor.LETVS_0).isSel = !isFail;
                    if (infor.btnHGPojo.get(infor.LETVS_0).isSel == true) {
                        infor.setOnclikNum(infor.getOnclikNum() + 1);
                    } else {
                        infor.setOnclikNum(infor.getOnclikNum() - 1);
                    }
                    onclikBtn();
                }
            }
        });

        if (infor.isShowHideView) {
            holder.ll_xiangqing.setVisibility(View.VISIBLE);
        } else {
            holder.ll_xiangqing.setVisibility(View.GONE);
        }
        if (infor.hideView != null) {

            ViewGroup parent1 = (ViewGroup) infor.hideView.getParent();
            if (parent1 != null) {
                parent1.removeAllViews();
            }
            holder.ll_xiangqing.removeAllViews();
            holder.ll_xiangqing.addView(infor.hideView);
        }

        // 点击显示隐藏信息
        holder.xianShi.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

//                if (infor.hideView == null) {
//                    infor.hideView = new FootballMatchSummaryView(context, infor.getMatchNum());
//                    holder.ll_xiangqing.removeAllViews();
//                    holder.ll_xiangqing.addView(infor.hideView);
//                }
//                infor.isShowHideView = !infor.isShowHideView;
//                if (infor.isShowHideView) {
//                    holder.ll_xiangqing.setVisibility(View.VISIBLE);
//                } else {
//                    holder.ll_xiangqing.setVisibility(View.GONE);
//                }

                Intent intent = new Intent(context, NewScoreDetailActivity.class);
                intent.putExtra("isFootball", true);
                intent.putExtra("matchNum", infor.getMatchNum());
                context.startActivity(intent);

            }

        });

//        holder.ll_xiangqing.setOnClickListener(new OnClickListener() {
//
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(context, ScoreDetailActivity.class);
//                intent.putExtra("matchNum", infor.getMatchNum());
//                context.startActivity(intent);
//            }
//        });
        return convertView;
    }

    private SpannableString getShowSP(String CONTENT) {
        RelativeSizeSpan span = new RelativeSizeSpan(Constants.scaleJCTxt);
        SpannableString spannableString = new SpannableString(CONTENT);
        spannableString.setSpan(span, 0, 2, Spanned
                .SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }


}
